package com.wwtech.epp.ui.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.ui.activity.work.component.DaggerEditTextComponent;
import com.wwtech.epp.ui.activity.work.contract.EditTextContract;
import com.wwtech.epp.ui.activity.work.module.EditTextModule;
import com.wwtech.epp.ui.activity.work.presenter.EditTextPresenter;
import com.wwtech.epp.utils.ToastUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/12/06 16:56:22
 */

public class EditTextActivity extends BaseActivity implements EditTextContract.View {

    @Inject
    EditTextPresenter mPresenter;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.clear_text)
    ImageView clearText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_edit_text);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initData() {
        setTitle(getIntent().getStringExtra("title"));
        etText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
//                KLog.e("输入过程中执行该方法", "文字变化");
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // 输入前的监听
//                KLog.e("输入前确认执行该方法", "开始输入");

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
//                KLog.e("输入结束执行该方法", "输入结束:" + s);
                if (s.length() == 0) {
                    clearText.setVisibility(View.GONE);
                } else {
                    clearText.setVisibility(View.VISIBLE);
                }
            }
        });
        etText.setText(getIntent().getStringExtra("content"));
        etText.setSelection(etText.getText().toString().length());


    }

    @Override
    protected void setupActivityComponent() {
        DaggerEditTextComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .editTextModule(new EditTextModule(this))
                .build()
                .inject(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.over_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.over:
                if (etText.getText().toString().trim().equals("")) {
                    ToastUtil.displayShortToast("群名称不能设置为空");
                    return false;
                }
                Intent intent = new Intent();
                intent.putExtra("content", etText.getText().toString().trim());
                setResult(20, intent);
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setPresenter(EditTextContract.EditTextContractPresenter presenter) {
        mPresenter = (EditTextPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @OnClick(R.id.clear_text)
    public void onViewClicked() {
        etText.setText("");
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.activity_translate_out_1);
    }
}